/*
 * @Descripttion:装修楼层预览入口：H5预览，APP预览，小程序预览
 * @Date: 2022-06-15 10:49:05
 * @LastEditTime: 2024-10-17 11:21:22
 */
import React from 'react'
import { useEffect, useState } from 'react'
import Taro from '@tarojs/taro'
import { BuiltInComponents } from '@modules/components'
import { ContainerFloorList } from '@conecli/cone-render/modules'
import { dealShopContentData } from '@conecli/cone-render/_utils'
import { Dialog } from '@conecli/cone-render/components'
import INTERFACE from '../../common/INTERFACE'
import './index.scss'

let keycount = 1

// 初始化
const Preview = (props) => {
  const [isLoading, setIsLoading] = useState(true)
  const [floorsContent, setFloors] = useState({
    containers: [],
    floors: [],
    pageSet: {},
  })
  const getAllFloors = () => {
    Taro.showLoading({
      title: '加载中',
    })
    Taro.request({
      url: INTERFACE.floorContent,
      method: 'POST',
      timeout: 7000,
      complete: function (res) {
        setIsLoading(false)
        Taro.hideLoading()
        const { data, statusCode } = res
        if (statusCode === 200 && data) {
          if (data.code == 200 && data.data) {
            keycount++
            setFloors(data.data)
          } else if (data.code == 401) {
            // 未登录
            window.location.href = `https://passport.jd.com/new/login.aspx?ReturnUrl=${encodeURIComponent(
              window.location.href,
            )}`
          }
        }
      },
    })
  }
  useEffect(() => {
    getAllFloors()
  }, [])

  const { shopContentContainerListData } = dealShopContentData(
    floorsContent,
    null,
    true,
  )
  if (isLoading) {
    return null
  } else {
    return (
      <>
        <ContainerFloorList
          {...props}
          key={keycount}
          builtInComponents={BuiltInComponents}
          containerFloorListData={shopContentContainerListData}
        />
        <Dialog />
      </>
    )
  }
}

export default Preview
